package com.dq.context.listener.user;

import com.dq.context.event.user.StaffUserServiceEvent;

import com.dq.domain.orders.Orders;
import com.dq.domain.orders.UserOrder;
import com.dq.domain.org.Org;
import com.dq.domain.record.ProfitRecord;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.setup.ReturnCommission;
import com.dq.domain.sys.user.Integral;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.order.UserOrderRepository;
import com.dq.repository.order.UserOrderStaffUserRepository;
import com.dq.repository.set.ReturnCommissionRepository;
import com.dq.repository.org.WorkRoyaltySetRepository;
import com.dq.repository.record.ProfitRecordRepository;
import com.dq.repository.record.RoyaltyRecordRepository;
import com.dq.repository.user.IntegralRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2019/12/3 14:11
 */
@Component
public class StaffUserServiceEventListener implements ApplicationListener<StaffUserServiceEvent> {
    @Autowired
    protected ReturnCommissionRepository returnCommissionRepository;
    @Autowired
    protected ProfitRecordRepository profitRecordRepository;
    @Autowired
    protected IntegralRepository integralRepository;
    @Autowired
    protected UserOrderRepository userOrderRepository;
    @Autowired
    protected UserOrderStaffUserRepository userOrderStaffUserRepository;
    @Autowired
    protected WorkRoyaltySetRepository workRoyaltySetRepository;
    @Autowired
    protected RoyaltyRecordRepository royaltyRecordRepository;


    @Override
    public void onApplicationEvent(StaffUserServiceEvent staffUserServiceEvent) {
        Map<String,Object> map = (Map) staffUserServiceEvent.getSource();
        UserOrder userOrder = (UserOrder) map.get("order");
        Long giveIntegral = (Long) map.get("integral");
        ReturnCommission(userOrder,giveIntegral);
//        Royalty(userOrder);
    }

    /**
     * 分润
     */
    public void ReturnCommission(Orders order,Long giveIntegral){
        UserOrder userOrder = (UserOrder) order;
        SysUser user = userOrder.getWxUser();
        //推荐人
        SysUser inviteUser =user.getInviteUser();
        SysSection section = null;
        if(inviteUser != null){
            section= inviteUser.getSection();
        }
        //提成比例
        ReturnCommission returnCommission = returnCommissionRepository.getBySysDeptAndDeletedFalseAndProportionType((Org) section,ReturnCommission.ProportionType.rakeBack);
        BigDecimal bili = new BigDecimal("0");
        if(returnCommission != null && returnCommission.getProportion() != null){
            bili = returnCommission.getProportion();
        }
        ProfitRecord profitRecord = new ProfitRecord();
        profitRecord.setUser(inviteUser);
        profitRecord.setChangeProfitType(ProfitRecord.ChangeProfitType.rakeBack);
        profitRecord.setProfitChangeType(ProfitRecord.ProfitChangeType.add);
        profitRecord.setChangeNum(userOrder.getPrice().multiply(bili));
        profitRecord.setUserOrder(userOrder);
        profitRecordRepository.save(profitRecord);
        //积分比例
        Integral integral = new Integral();
        integral.setUser(user);
        integral.setCiType(Integral.ChangeIntegralType.buyinteg);
        integral.setChangeType(Integral.ChangeType.add);
        integral.setChangeNum(giveIntegral);
        integral.setUserOrder(userOrder);
        integralRepository.save(integral);
    }

    /**
     * 服务提成  提成记录
     */
    /*public void  Royalty(UserOrder order){
        List<UserAccountStaffUser> u = userOrderStaffUserRepository.findByUserOrderAndDeletedFalse(order);
        List<SysUser> list = new ArrayList<>();
        u.forEach(userOrderStaffUser -> {
            list.add(userOrderStaffUser.getStaffUser());
        });
        list.forEach(sysUser -> {
            StaffUser staffUser = (StaffUser) sysUser;
            WorkRoyaltySet workRoyaltySet = workRoyaltySetRepository.findByOrgAndOrgPositionAndTopLimitPriceLessThanEqualAndLowerLimitPriceGreaterThanEqualAndDeletedFalse(staffUser.getOrg(),staffUser.getOrgPosition(),order.getPrice(),order.getPrice());
            BigDecimal proportion = new BigDecimal("0");
            if(workRoyaltySet != null && workRoyaltySet.getProportion() != null){
                proportion = workRoyaltySet.getProportion();
            }
            RoyaltyRecord record = new RoyaltyRecord();
            record.setOrg(sysUser.getOrg());
            record.setSysUser(sysUser);
            record.setOrders(order);
            record.setRoyaltyPrice(proportion.multiply(order.getPrice()));
            royaltyRecordRepository.save(record);
        });
    }*/

}
